package hc.bre.cmn

import hc.bre.rest.admin.PermissionType

enum DiffType {
	created('新建'),
	deleted('删除'),
	updated('更新'),
	obsolote('过期'),
	identical('相同'),
	conflicted('冲突')

	String desc
	DiffType(String name , int ordinal, String desc) {
		super(name,ordinal);
		this.desc = desc;
	}

	static Map<String,String> map() {
		Map<String,String> lst = [:];
		DiffType.values().each{
			lst.put(it.name(),it.desc);
		}
		return lst;
	}
}

class DiffPair{
	RuleDefinition left
	RuleDefinition right

	public static List<DiffPair> buildPairList(List<RuleDefinition> leftList , List<RuleDefinition> rightList) {
		if(leftList==null)leftList=[];
		if(rightList==null)rightList=[];
		List<DiffPair> list = [];

		LinkedHashMap<String,RuleDefinition> rightMap = new LinkedHashMap<String,RuleDefinition>()
		rightList.each {
			rightMap.put(it.rpath, it);
		}

		leftList.each {
			def right = rightMap.get(it.rpath);
			list.add(new DiffPair(left:it,right:right));
			rightMap.remove(it.rpath)
		}

		rightMap.values.each {
			list.add(new DiffPair(left:null,right:it))
		}

		return list;
	}
}

class DiffResult<T> {
	DiffType type
	T self
	T other
	String msg
	List<DiffResult<?>> list;

	DiffResult<T> add(DiffType type,Object self,Object other, String msg='') {
		if(list == null) list = [];
		list.add(new DiffResult<Object>(type:type,self:self,other:other,msg:msg));
		return this;
	}

	DiffResult<T> addIfDiff(DiffResult<?> result) {
		if(result==null || result.type == DiffType.identical)
			return this;

		if(list == null) list = [];
		list.add(result);

		return this;
	}
}
